﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data.SqlClient;
using System.Data;

namespace DataAccess.Operate
{
    public class DA_Project_Inherit : SqlInteractionBase
    {
        #region 生成序号
        public int genProjectIndex()
        {
            string sProjectNO = "";
            int iProjectIdx = 0;

            if (DateTime.Now.Month > 9)
            { sProjectNO = "项目部" + DateTime.Now.Year.ToString().Substring(2) + DateTime.Now.Month.ToString(); }
            else
            { sProjectNO = "项目部" + DateTime.Now.Year.ToString().Substring(2) + "0" + DateTime.Now.Month.ToString(); }

            string sSql = "select isnull(max(Project_ProjectNOIdx),0)+1 as Project_ProjectNOIdx from t_Project where Project_NO like '" + sProjectNO + "%'";
            SqlCommand cmdToExecute = new SqlCommand();
            SqlDataAdapter adapter = new SqlDataAdapter(cmdToExecute);
            DataSet ds = new DataSet();

            cmdToExecute.CommandText = sSql;
            cmdToExecute.CommandType = CommandType.Text;

            try
            {
                cmdToExecute.Connection = _mainConnection;
                if (_mainConnectionIsCreatedLocal)
                {
                    _mainConnection.Open();
                }
                else
                {
                    if (_mainConnectionProvider.IsTransactionPending)
                    {
                        cmdToExecute.Transaction = _mainConnectionProvider.CurrentTransaction;
                    }
                }

                adapter.Fill(ds);
                if (ds.Tables[0].Rows.Count > 0)
                { iProjectIdx = Convert.ToInt32(ds.Tables[0].Rows[0]["Project_ProjectNOIdx"].ToString()); }
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message.ToString());
            }
            finally
            {
                if (_mainConnectionIsCreatedLocal)
                {
                    _mainConnection.Close();
                }

                cmdToExecute.Dispose();

                adapter.Dispose();
            }

            return iProjectIdx;
        }
        #endregion

        #region 生成文件编号
        public string genProjectNO()
        {
            string sProjectNO = "", sProectIndex = "";

            if (DateTime.Now.Month > 9)
            { sProjectNO = "项目部" + DateTime.Now.Year.ToString().Substring(2) + DateTime.Now.Month.ToString(); }
            else
            { sProjectNO = "项目部" + DateTime.Now.Year.ToString().Substring(2) + "0" + DateTime.Now.Month.ToString(); }

            string sSql = "select isnull(max(Project_ProjectNOIdx),0)+1 as Project_ProjectNOIdx from t_Project where Project_NO like '" + sProjectNO + "%'";
            SqlCommand cmdToExecute = new SqlCommand();
            SqlDataAdapter adapter = new SqlDataAdapter(cmdToExecute);
            DataSet ds = new DataSet();

            cmdToExecute.CommandText = sSql;
            cmdToExecute.CommandType = CommandType.Text;

            try
            {
                cmdToExecute.Connection = _mainConnection;
                if (_mainConnectionIsCreatedLocal)
                {
                    _mainConnection.Open();
                }
                else
                {
                    if (_mainConnectionProvider.IsTransactionPending)
                    {
                        cmdToExecute.Transaction = _mainConnectionProvider.CurrentTransaction;
                    }
                }

                adapter.Fill(ds);
                if (ds.Tables[0].Rows.Count > 0)
                { sProectIndex = this.genIndxe(Convert.ToInt32(ds.Tables[0].Rows[0]["Project_ProjectNOIdx"].ToString())); }
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message.ToString());
            }
            finally
            {
                if (_mainConnectionIsCreatedLocal)
                {
                    _mainConnection.Close();
                }

                cmdToExecute.Dispose();

                adapter.Dispose();
            }

            return sProjectNO + sProectIndex;
        }
        #endregion

        public string genIndxe(int iIndex)
        {
            string sIndex = "";

            if (iIndex > 9 && iIndex < 100)
            { sIndex = "0" + iIndex.ToString(); }
            else if (iIndex >= 100)
            { sIndex = iIndex.ToString(); }
            else if (iIndex < 10)
            { sIndex = "00" + iIndex.ToString(); }

            return sIndex;
        }


        /// <summary>
        /// 审批操作
        /// </summary>
        /// <param name="mainID"></param>
        /// <param name="audit"></param>
        /// <returns></returns>
        public bool AuditByID(string mainID, string audit)
        {
            string sSql = "update t_Project set Project_Audit=" + audit + " where Project_ID='"+mainID+"'";
            return RunNoneSQL(sSql);
        }

        /// <summary>
        /// 返回项目名称
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public string getProjectName(string id)
        {
            string sSql = "select Project_Name from t_Project where Project_ID='"+id+"'";
            string toReturn = "";
            DataTable dt = RunSQL(sSql).Tables[0];
            if (dt.Rows.Count > 0)
            {
                toReturn = dt.Rows[0][0].ToString();
            }
            return toReturn;
        }

        /// <summary>
        /// 获取指定项目的单个审批人工号和姓名
        /// </summary>
        /// <param name="operate"></param>
        /// <param name="projectID"></param>
        /// <returns></returns>
        public string[] getCodeAndName(out string outProject_NO, string operate, string projectID)
        {
            string[] toReturn=new string[2];
            outProject_NO = "";
            string sSql = "select Project_NO,Project_DirecterCode,Project_Directer,Project_Directer1Code,Project_Directer1,Project_ManagerCode,Project_Manager,Project_Business,Project_BusinessCode,Project_DepartmentHead,Project_DepartmentHeadCode  from  t_Project where Project_ID='" + projectID + "'";
            DataTable dt= RunSQL(sSql).Tables[0];
            if (dt.Rows.Count > 0)
            {
                outProject_NO = dt.Rows[0]["Project_NO"].ToString();
                if (operate == "5") //对照数据库[t_Dic_Project_Operate]表
                {
                    toReturn[0] = dt.Rows[0]["Project_Directer1Code"].ToString();
                    toReturn[1] = dt.Rows[0]["Project_Directer1"].ToString();
                }
                else if (operate == "6")
                {
                    toReturn[0] = dt.Rows[0]["Project_DepartmentHeadCode"].ToString();
                    toReturn[1] = dt.Rows[0]["Project_DepartmentHead"].ToString();
                }
                else if (operate == "8")
                {
                    toReturn[0] = dt.Rows[0]["Project_BusinessCode"].ToString();
                    toReturn[1] = dt.Rows[0]["Project_Business"].ToString();
                }
                else if (operate == "10")
                {
                    toReturn[0] = dt.Rows[0]["Project_ManagerCode"].ToString();
                    toReturn[1] = dt.Rows[0]["Project_Manager"].ToString();
                }
            }
            return toReturn;
        }

        /// <summary>
        /// 获取指定项目的全部审批人工号和姓名
        /// </summary>
        /// <param name="projectID"></param>
        /// <returns></returns>
        public DataTable getCodeAndName(string projectID)
        {
            string sSql = "select Project_DirecterCode,Project_Directer,Project_Directer1Code,Project_Directer1,Project_ManagerCode,Project_Manager,Project_Business,Project_BusinessCode,Project_DepartmentHead,Project_DepartmentHeadCode  from  t_Project where Project_ID='"+projectID+"'";
            return RunSQL(sSql).Tables[0];
        }


    }
}
